// Async/await是Promise的语法糖。类似于generatorAsync.js里面的co函数一样。只是EcmaScript在es2017这个版本在语言层面标准化了

function ajax(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.responseType = 'json';
    xhr.onload = function () {
      if (this.status === 200) {
        resolve(this.response);
      } else {
        reject(this.statusText);
      }
    };
    xhr.send();
  });
}

// 注意async函数本身也是返回promise
async function main() {
  const person = await ajax('/api/cuvee.json');
  console.log(person);
  const detail = await ajax('/api/info.json');
  console.log(detail);
}

main();
